package com.example.administrator.androidros.fragment;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.Toast;

import com.example.administrator.androidros.R;
import com.example.administrator.androidros.adapter.SwipeMenuAdapter;
import com.example.administrator.androidros.base.BaseFragment;
import com.hhc.netty.DeviceModel;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by huhaichao on 2018/3/12.
 */

public class TaskFragment extends BaseFragment {

    private LRecyclerView mRecyclerView = null;

    private SwipeMenuAdapter mDataAdapter = null;

    private LRecyclerViewAdapter mLRecyclerViewAdapter = null;

    public static TaskFragment newInstance() {
        return new TaskFragment();
    }

    @Override
    protected void initData(View view, Bundle savedInstanceState) {
        mRecyclerView = (LRecyclerView) view.findViewById(R.id.fragment_message_list);
        mDataAdapter = new SwipeMenuAdapter(mBaseActivity);
        List<DeviceModel> data1 = new ArrayList();
        for (int i = 0; i < 3; i++) {
            DeviceModel deviceModel = new DeviceModel();
            deviceModel.setApplianceType("10");
            deviceModel.setApplianceName("胡海潮" + i);
            deviceModel.setOnlineStatus("10");
            deviceModel.setStatus("10");
            data1.add(deviceModel);
        }
        for (int i = 3; i < 6; i++) {
            DeviceModel deviceModel = new DeviceModel();
            deviceModel.setApplianceType("10");
            deviceModel.setApplianceName("胡海潮" + i);
            deviceModel.setOnlineStatus("20");
            deviceModel.setStatus("20");
            data1.add(deviceModel);
        }
        mDataAdapter.setDataList(data1);

        mDataAdapter.setOnDelListener(new SwipeMenuAdapter.onSwipeListener() {
            @Override
            public void onDel(int pos) {
                Toast.makeText(mBaseActivity, "删除:" + pos, Toast.LENGTH_SHORT).show();
                mDataAdapter.getDataList().remove(pos);
                mDataAdapter.notifyItemRemoved(pos);//推荐用这个
                if (pos != (mDataAdapter.getDataList().size())) { // 如果移除的是最后一个，忽略 注意：这里的mDataAdapter.getDataList()不需要-1，因为上面已经-1了
                    mDataAdapter.notifyItemRangeChanged(pos, mDataAdapter.getDataList().size() - pos);
                }
            }
        });

        mLRecyclerViewAdapter = new LRecyclerViewAdapter(mDataAdapter);
        mRecyclerView.setAdapter(mLRecyclerViewAdapter);

        mRecyclerView.setLayoutManager(new LinearLayoutManager(mBaseActivity));
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_message;
    }

    @Override
    protected void onFailure(int requestCode, Object object) {

    }

    @Override
    protected void onSuccess(int requestCode, Object object) {

    }
}
